package com.aabte.springcloudlearn.ribbonhystrixcustomerdemo.controller;

import com.aabte.springcloudlearn.common.beans.User;
import com.aabte.springcloudlearn.ribbonhystrixcustomerdemo.service.ObservableCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import rx.Observable;

/**
 * @author Daniel
 */
@Slf4j
@RestController
public class ObservableCustomerController {

    @Autowired
    private ObservableCustomerService service;

    @GetMapping("/annotation-observable/user/{id:\\d+}")
    public User sendAnnotationObservableRequestGetUserById(
            @PathVariable(name = "id") Long userId){
        log.info("Start sendAnnotationObservableRequestGetUserById. userId: {}", userId);
        Observable<User> userObservable = service.useObserveByAnnotation(userId);
        User user = userObservable.toBlocking().first();
        log.info("End sendAnnotationObservableRequestGetUserById. user: {}", user);
        return user;
    }

    @GetMapping("/observe/user/{id:\\d+}")
    public User sendObserveRequestGetUserById(
            @PathVariable(name = "id") Long userId){
        log.info("Start sendObserveRequestGetUserById. userId: {}", userId);
        Observable<User> userObservable = service.useObserveRequestGetUserById(userId);
        User user = userObservable.toBlocking().first();
        log.info("End sendObserveRequestGetUserById. user: {}", user);
        return user;
    }

    @GetMapping("/toObservable/user/{id:\\d+}")
    public User sendToObservableRequestGetUserById(
            @PathVariable(name = "id") Long userId){
        log.info("Start sendToObservableRequestGetUserById. userId: {}", userId);
        Observable<User> userObservable = service.useToObservableRequestGetUserById(userId);
        User user = userObservable.toBlocking().first();
        log.info("End sendToObservableRequestGetUserById. user: {}", user);
        return user;
    }


}
